var payinfo = {};
var us = {
    money:0,
    shopMoney:0,
    score:0,
	isGoPay:0,
};
$(function() {
    if(!getUserinfo()) return false;
    var b = function() {
        var userinfo = getUserinfo();
        var jsonData = localData.get('f_cart');
        
        PostData(config.url+'/app.php?act=fpayment&uid='+userinfo.uid,{
            'json' : jsonData
        },function(data){
           if (data['status'] == 1) {
               $('.comm-loading').hide();
               var html = '';
               for(var i in data['shoplist']){
                html += '<li><a href="javascript:openItem(\'f_item\','+data['shoplist'][i]['id']+',function(id){localData.set(\'f_goods_id\',id);});" class="gray6">(第'+data['shoplist'][i]['qishu']+'期)'+data['shoplist'][i]['title']+'  ('+data['shoplist'][i]['title2']+')</a>';
                html += '<span><em class="orange arial">'+data['shoplist'][i]['cart_gorenci']+'</em>人次';
                html += '</span></li>';
               }
               $('section.g-pay-lst .comm-loading').before(html);
               
               $('#shopnum').val(data['count']);
               $('#hidBalance').val(data['money']);
               $('.myscore').text(data['hidPoints']);
               $('#hidShopMoney').val(data['MoenyCount']);
			   $('.fxd').text(data['MoenyCount']);
			   $('.fxe').text(data['all_inviteNum']);
               $('.shoPMoenyCount').text(data['MoenyCount']);
               $('#hidPoints').val(data['hidPoints']);
               us.money = data['money'];
               us.score = data['hidPoints'];
			   $('.invite_num').text(data['invite_num']);
			   
			   $('#spBalance').parent().show();
			   if(data['invite_num'] >= data['all_inviteNum']){
			   	 $('#spBalance').removeClass('z-pay-ment');
			   	 $('#spBalance').addClass('z-pay-mentsel');
			   }
			   
               if(data['MoenyCount'] > data['money']){
                   us.shopMoney = data['MoenyCount']-data['money'];
               }else{
                   us.shopMoney = data['MoenyCount'];
               }
               $('.MoenyCount').text(us.shopMoney);
               $('.bag_money').text('您的余额不足（账户余额：'+data['money']+'元）');
               $('.payplatform').after(data['paylist']);
               if(!data['paylist']){
			   	 us.isGoPay = 1;
				 $('.payplatform').hide();
				 $('#ulPayway').hide();
				 
			   }
			   
               exc();
           }else{
               $.PageDialog.fail(data['msg']);
               //appClose('payment');
           }; 
        });
        
        function exc(){
            var a = false;
            var x = parseInt($("#hidShopMoney").val());
            var ffdk = parseInt($("#pointsbl").val());
            var d = $("#hidBalance").val();
            var t = parseInt($("#hidPoints").val());
            var c = $("#spPoints");
            var p = $("#spBalance");
            var h = null;
            var shopnum = parseInt($("#shopnum").val());
            var r = "招商银行";
            //var g = parseInt(t / 100) > x ? x: parseInt(t / 100);
            var g = ffdk > x ? x: ffdk;
            var w = 0;
            var e = 0;
            var checkpay='nosel';//选择支付方式
            var banktype='nobank';
    
            if (g < x) {
                var j = parseInt(d);
                if (j > 0) {
                    var i = x - g;
                    if (j >= i) {
                        w = i;
                    } else {
                        w = j;
                        e = i - j;
                    }
                } else {
                    e = x - g;
                }
            }
    
            var q = function(y) {
                g = y;
                if (y > 0) {
                    c.parent().removeClass("z-pay-grayC");
                    c.attr("sel", "1").attr("class", "z-pay-mentsel").next("span").html('福分支付<em class="orange">' + y + ".00</em>元（您的福分：" + t + "）");
                    checkpay='fufen';
                    banktype='nobank';
                } else {
                    c.attr("sel", "0").attr("class", "z-pay-ment").next("span").html('福分支付<em class="orange">0.00</em>元（您的福分：' + t + "）");
                }
            };
            var f = function(y) {
                w = y;
                if (y > 0) {
                    p.parent().removeClass("z-pay-grayC");
                    //p.attr("sel", "1").attr("class", "z-pay-mentsel").next("span").html('余额支付<em class="orange">' + y + ".00</em>元（账户余额：" + d + " 元）");
                    checkpay='money';
                    banktype='nobank';
                } else {
                    //p.attr("sel", "0").attr("class", "z-pay-ment").next("span").html('余额支付<em class="orange">0.00</em>元（账户余额：' + d + " 元）");
                }
            };
            var k = function(y) {
            };
            if (ffdk > 0) {
                c.parent().click(function() {
                    if (c.attr("sel") == 1) {
                        q(0);
                        n(x);
                    } else {
                        var y = ffdk;
                        if (y > 0) {
                            q(y >= x ? x: y);
                            n(y >= x ? 0 : x - y);
                        } else {
                            n(x);
                        }
                    }
                });
                var n = function(z) {
                    if (p.attr("sel") == 1) {
                        var y = parseInt(d) - z;
                        if (y > 0) {
                            f(z);
                        } else {
                            f(parseInt(d));
                        }
                    }
                };
            };
    
            if (parseInt(d) > 0) {
                p.parent().click(function() {
    
                    k(0);
                    if (p.attr("sel") == 1) {
                        f(0);
                        l(x);
                    } else {
                        var y = parseInt(d);
                        if (y > 0) {
                            f(y >= x ? x: y);
                            l(y >= x ? 0 : x - y);
                        } else {
                            l(x);
                        }
                    }
                });
                var l = function(z) {
                    if (c.attr("sel") == 1) {
                        var y = ffdk - z;
                        if (y > 0) {
                            q(z);
                        } else {
                            q(ffdk);
                            k( - y);
                        }
                    } else {
                        k(z);
                    }
                };
            };
    
            if ( c.length > 0 ) {
                c.parent().click();
                p.parent().unbind("click");
                c.parent().unbind("click");
            }else if ( p.length > 0 ) {
                p.parent().click();
                p.parent().unbind("click");
                c.parent().unbind("click");
    
            }
    
            var o = false;
            var v = 1;
            var s = $("#btnPay");
            var u = function() {
                var submitcode = Path.submitcode;
                if (!a) {
                    return;
                }
                //if (w + g >= x) {

                    if (shopnum != -1) {
                     a = false;
                    s.unbind("click").addClass("dis");
                        if(shopnum == 0){
                            $.PageDialog.fail('购物车中没有商品哦');
                            localData.remove('f_cart');
                            localData.remove('f_cart_num');
                            return;
                        }else{
                            //checkpay 支付类型 money 余额支付
                            //banktype 选择支付平台 微信支付，支付宝支付
                            //t 积分
                            //x 金额
                           $('#btnPay').addClass('btnPay');
                           $('.actionPay').addClass('private');
                            api.showProgress({title:'正在提交',text:''});
                            PostData(config.url+'/app.php?act=fpaysubmit&uid='+userinfo.uid,{
                                submit:true,bag:'b',checkpay:checkpay,banktype:banktype,
                                money:x,score:t,cart:localData.get('f_cart'),
                            },function(data){
                                api.hideProgress();
                                localData.remove('f_cart');
                                localData.remove('f_cart_num');
                                if(data['status']){
                                    $('#btnPay').hide();
                                    $('.actionPay').hide();
                                    api.alert({
                                        title:'提示',msg:data['msg'],
                                    },function(index,err){
                                        randomSwitchBtn('paysuccess',api);
                                        appClose('f_cart',api);
                                        appClose('f_payment',api);
                                    });
                                }else{
                                    $('#btnPay').removeClass('btnPay');
                                    $('.actionPay').removeClass('private');
                                    $('.actionPay').hide();
                                    api.alert({title:'提示',msg:data['msg']},function(index,err){
                                        appClose('f_payment',api);
                                    });
                                }
                            });
                        }
                    }
                    s.bind("click", u).removeClass("dis");
                    a = true;
                /*
                } else {
                    if (e > 0) {
                        if (v == 1 || v == 2 || v == 3) {
                               //location.href = Gobal.Webpath+"/mobile/cart/paysubmit/"+checkpay+"/"+banktype+"/"+x+"/"+t
                        }
                    }
                }
                */
            };
            s.bind("click", u);
            a = true;
     }
        
    };
    b();
});